<?php
/**
 * Author: Nguyen Hoai Tan (nguoiao007@gmail.com)
 * 
 * @category   default 
 * @copyright  Copyright (c) 2009 visualidea.org
 * @license    http://license.visualidea.org
 * @version    v 1.0 2009-04-15
 * 
 */
require_once 'modules/user/models/User.php';
require_once 'modules/user/models/Wallet.php';
require_once 'modules/transaction/models/Transaction.php';
class user_IndexController extends Nine_Controller_Action
{
    public $userId;
	
	public function viewWalletAction()
    {
    	
    	$this->_checkLogin();
    	
    	$this->view->headTitle(Nine_Language::translate('View Wallet'));
    	if (2 == Nine_Registry::getLoggedInGroupId()) {
    		$this->view->menuId = 'merchant';
    	}
    	else if(3 == Nine_Registry::getLoggedInGroupId()) {
    		$this->view->menuId = 'consumer';
    	}
    	
    	
    	$objWallet = new Models_Wallet();
    	$objTransaction = new Models_Transaction();
    	
    	$wallet = @$objWallet->getWalletByUserId($this->userId)->toArray();
    	if(null != @$wallet['value']) {
    		$tmp = $wallet['value'];
    		$wallet['value'] = number_format($tmp);
    		$wallet['minimum_value'] = number_format(Nine_Registry::getConfig('minimumBalance'));
    		$wallet['valid_value'] = number_format($tmp - Nine_Registry::getConfig('minimumBalance'));
    	}
    	
//    	$report = $objTransaction->getTransactionReportPerMonthByUserId($this->userId);
//    	
//    	/**
//    	 * Modified
//    	 */
//    	if (!empty($report)) {
//    		foreach($report as &$r) {
//				$r['created_date'] = date('d-m-Y H:i:s A',$r['created_date']);
//    			if( 0 === $r['type'] || 3 == $r['type']) {
//    				$r['amount'] = '-' . number_format($r['amount']) . ' VNĐ';
//    			}
//    			else {
//    				$r['amount'] = '+' . number_format($r['amount']) . ' VNĐ';
//    			}
//    		}
//    		unset($r);
//    		
//    	}
    	
    	$this->view->wallet = $wallet;
//    	$this->view->report = $report;
    	
    }
    
    private function _checkLogin()
    {
    	if (null != @Nine_Registry::getLoggedInUser()) {
    		$this->userId = Nine_Registry::getLoggedInUserId();
    	}
    	else {
    		$this->_redirect('access/index/login');
    	}
    }
}